<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧图标和文字 -->
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="warning" plain @click="logout">退出登录</el-button>
    </el-header>
    <!-- 内容区域 -->
    <el-container>
      <!-- 左侧边栏区域 -->
      <el-aside :width="isCollapse?'60px':'200px'">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          unique-opened
          active-text-color="#ffd04b"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <!-- 一级导航栏 -->
          <div class="toggle-button" @click="toggleMenu">|||</div>
          <el-submenu :index="'/'+item.path" v-for="item in list" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/'+childList.path"
              v-for="childList in item.children"
              :key="childList.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{childList.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  created() {
    // 在这里调用获取数据的方法
    this.getMenus()
  },
  data() {
    return {
      list: [],
      isCollapse: false,
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  methods: {
    logout() {
      this.$router.push('/login')
      window.sessionStorage.clear()
    },
    // 这里定义的是调用后台数据的方法，当时没有在页面结构中调用，而是在created函数的最早调用axios数据
    async getMenus() {
      // 获取后台数据，同时通过拦截器将token添加到请求头中
      const { data: res } = await this.$http.get('/menus')
      // 当数据获取成功的时候，可以将其添加到页面存储中然后通过遍历在页面中进行展示
      if (res.meta.status === 200) {
        this.list = res.data
      }
    },
    toggleMenu() {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    span {
      color: #fff;
      font-size: 16px;
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .toggle-button {
    height: 30px;
    line-height: 30px;
    background-color: #333744;
    color: #fff;
    margin: 0 auto;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .el-menu {
    border: none;
    .el-submenu {
      span {
        margin-left: 10px;
      }
    }
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
